package com.taotao.portal.service.ipml;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.common.utils.JsonUtils;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.portal.service.IItemParamItemService;

@Service
public class ItemParamItemServiceIpml implements IItemParamItemService {
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;

	@Value("${ITEM_PARAM_URL}")
	private String ITEM_PARAM_URL;

	@Override
	public String getItemParamItemByItemId(Long itemId) {
		// TODO Auto-generated method stub
		String doGet = HttpClientUtil.doGet(REST_BASE_URL + ITEM_PARAM_URL
				+ itemId);
		if (StringUtils.isNotBlank(doGet)) {
			TaotaoResult taotaoResult = TaotaoResult.formatToPojo(doGet,
					TbItemParamItem.class);
			TbItemParamItem itemParamItem = (TbItemParamItem) taotaoResult
					.getData();
			List<Map> mapList = null;
			if (itemParamItem != null) {
				String paramData = itemParamItem.getParamData();
				mapList = JsonUtils.jsonToList(paramData, Map.class);
			} else {
				return null;
			}
			StringBuilder sb = new StringBuilder();
			sb.append("<table class=\"Ptable\" width=\"100%\" cellspacing=\"1\" cellpadding=\"0\" border=\"0\">"
					+ "\n");
			sb.append("\t" + "<tbody>" + "\n");
			for (Map tbItemParamItem : mapList) {
				sb.append("\t" + "\t" + "<tr>" + "\n");
				sb.append("\t" + "\t" + "\t"
						+ "<th class=\"tdTitle\" colspan=\"2\">"
						+ tbItemParamItem.get("group") + "</th>" + "\n");
				sb.append("\t" + "\t" + "</tr>" + "\n");
				List<Map<String, String>> paramsMapList = (List<Map<java.lang.String, java.lang.String>>) tbItemParamItem
						.get("params");
				for (Map<String, String> paramsMap : paramsMapList) {

					sb.append("\t" + "\t" + "<tr>" + "\n");
					sb.append("\t" + "\t" + "\t" + "<td class=\"tdTitle\">"
							+ paramsMap.get("k") + "</td>" + "\n");
					sb.append("\t" + "\t" + "\t" + "<td>" + paramsMap.get("v")
							+ "</td>" + "\n");
					sb.append("\t" + "\t" + "</tr>" + "\n");
				}
			}
			sb.append("\t" + "</tbody>" + "\n");
			sb.append("</table>" + "\n");

			return sb.toString();
		}
		return "";
	}

}
